package com.basics.framework.manager.factory;

import com.basics.common.utils.LogUtils;
import com.basics.common.utils.ServletUtils;
import com.basics.common.utils.ip.AddressUtils;
import com.basics.common.utils.ip.IpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.TimerTask;

public class AsyncFactory {
    private static final Logger sys_user_logger = LoggerFactory.getLogger("sys-user");

    public AsyncFactory() {
    }

    public static TimerTask recordLogininfor(final String username, final String status, final String message, final Object... args) {
        //final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        final String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
        return new TimerTask() {
            public void run() {
                String address = AddressUtils.getRealAddressByIP(ip);
                StringBuilder s = new StringBuilder();
                s.append(LogUtils.getBlock(ip));
                s.append(address);
                s.append(LogUtils.getBlock(username));
                s.append(LogUtils.getBlock(status));
                s.append(LogUtils.getBlock(message));
                AsyncFactory.sys_user_logger.info(s.toString(), args);
                //String os = userAgent.getOperatingSystem().getName();
                //String browser = userAgent.getBrowser().getName();
              /*  AppLogininfor logininfor = new AppLogininfor();
                logininfor.setUserName(username);
                logininfor.setIpAddr(ip);
                logininfor.setLoginLocation(address);
                logininfor.setBrowserType(browser);
                logininfor.setInfoOs(os);
                logininfor.setInfoMsg(message);
                if (!"Success".equals(status) && !"Logout".equals(status)) {
                    if ("Error".equals(status)) {
                        logininfor.setInfoStatus(Constants.FAIL);
                    }
                } else {
                    logininfor.setInfoStatus(Constants.SUCCESS);
                }

                ((AppLogininforService)SpringUtils.getBean(AppLogininforService.class)).saveOrUpdate(logininfor);*/
            }
        };
    }

  /*  public static TimerTask recordOper(final AppOperLog operLog) {
        return new TimerTask() {
            public void run() {
                operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp()));
                ((AppOperLogService)SpringUtils.getBean(AppOperLogService.class)).save(operLog);
            }
        };
    }*/
}
